package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import Client.BusinessImplements;
import Controler.Add_New_User_Controler;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
 * @author Lucie
 * Class name: Add_New_User_Panel
 * extend  Abstract_Jpanel
 * purpose: this panel get a user id,email,role from the user and call the controller 
 * to add new user to the data base
 */
public class Add_New_User_Panel extends Abstract_Jpanel {
	private JTextField textField_ID;
	private JTextField textFieldEmail;
	private JComboBox comboBoxRole;

	/**
	 * Create the panel.
	 */
	public Add_New_User_Panel() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(42, 64, 734, 321);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(105, 34, 513, 244);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblUserId = new JLabel("user id:");
		lblUserId.setBounds(69, 28, 52, 31);
		panel_1.add(lblUserId);
		lblUserId.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textField_ID = new JTextField();
		textField_ID.setBounds(130, 35, 150, 20);
		panel_1.add(textField_ID);
		textField_ID.setColumns(10);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(130, 86, 150, 20);
		panel_1.add(textFieldEmail);
		textFieldEmail.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("email:");
		lblNewLabel.setBounds(69, 84, 52, 20);
		panel_1.add(lblNewLabel);
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		 comboBoxRole = new JComboBox();
		 comboBoxRole.setBounds(130, 137, 150, 20);
		 panel_1.add(comboBoxRole);
		 comboBoxRole.setModel(new DefaultComboBoxModel(new String[] {"", "librarian", "maneger"}));
		 
		 JLabel lblRole = new JLabel("role:");
		 lblRole.setBounds(69, 138, 46, 14);
		 panel_1.add(lblRole);
		 lblRole.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		JLabel lblAddNewUser = new JLabel("Add New User");
		lblAddNewUser.setForeground(new Color(25, 25, 112));
		lblAddNewUser.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblAddNewUser.setBounds(42, 21, 517, 32);
		add(lblAddNewUser);
		/*
		 * btnBack ;When you click this button you will return to 
		 * sing in panel screen
		 */
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();

			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(70, 417, 89, 32);
		add(btnBack);
		/*
		 * btnAdd=When you click this button you will call add_User(user id,email,role)
		 * a method of the controller of this panel if this method return true we will
		 * call the methods of this class next_screen_success(),empty_text_fields();
		 * else we call next_screen_faild(add_new_item_controler1.get_msg());	
		 * 	add_new_user_controler1.get_msg(); return the msg from the controller with all
		 * the error msg.
		 */
		JButton btnAdd = new JButton("add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Add_New_User_Controler add_new_user_controler1=new Add_New_User_Controler();
				if(add_new_user_controler1.add_User(textField_ID.getText(),textFieldEmail.getText(),get_role()))
				{
					next_screen_success();
					empty_text_fields();
					
				}
				else {
					next_screen_faild(add_new_user_controler1.get_msg());
					
				}
				
			}
		});
		btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnAdd.setBounds(671, 417, 89, 32);
		add(btnAdd);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 * this method display showMessageDialog with a faild msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	/**
	 * this method empty all the text field
	 */
	public void empty_text_fields()
	{
		textField_ID.setText(null);
		textFieldEmail.setText(null);
		//comboBoxRole.setItemAt(0);
		on_screen_load();
	}
	public String get_role()
	{
	if(comboBoxRole.getSelectedItem().toString().equals("librarian"))
		return "2";
	if(comboBoxRole.getSelectedItem().toString().equals("maneger"))
	return"3";
	return "";
	}
}
